package com.ruoyi.common.utils.email;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSendException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 邮件发送服务
 */
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public class EmailServer {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;


    /**
     * 发送普通邮件
     * @param tos
     * @param cc
     * @param subject
     * @param text
     */
    public void send(String[] tos, List<String> cc, String subject, String text) {
        this.send(tos, cc, subject, text, (List)(new ArrayList()));
    }

    /**
     * 发送附件
     * @param tos
     * @param cc
     * @param subject
     * @param text
     * @param attachments
     */
    public void send(String[] tos, List<String> cc, String subject, String text, List<File> attachments) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = null;
            if (attachments != null && attachments.size() > 0) {
                mimeMessageHelper = new MimeMessageHelper(mimeMessage, 1, "UTF-8");
            } else {
                mimeMessageHelper = new MimeMessageHelper(mimeMessage, "UTF-8");
            }

            mimeMessageHelper.setTo(tos);
            if (cc != null) {
                mimeMessageHelper.setCc((String[])cc.toArray(new String[0]));
            }
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setFrom(this.from);
            mimeMessageHelper.setText(text, true);
            if (attachments != null && attachments.size() > 0) {
                Iterator i$ = attachments.iterator();

                while(i$.hasNext()) {
                    File file = (File)i$.next();
                    mimeMessageHelper.addAttachment(file.getName(), file);
                }
            }

            javaMailSender.send(mimeMessage);
        } catch (Exception var10) {
            throw new MailSendException("邮件发送失败！", var10);
        }
    }

    /**
     * 发送模板邮件
     * @param tos
     * @param cc
     * @param subject
     * @param viewPath
     * @param dataMap
     */
    public void send(String[] tos, List<String> cc, String subject, String viewPath, Map<String, Object> dataMap) {
        this.send(tos, cc, subject, viewPath, dataMap, (List)null);
    }

    /**
     * 发送模板加附件
     * @param tos
     * @param cc
     * @param subject
     * @param viewPath
     * @param dataMap
     * @param attachments
     */
    public void send(String[] tos, List<String> cc, String subject, String viewPath, Map<String, Object> dataMap, List<File> attachments) {
        try {
            // 开始配置FreeMarker邮件模板
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
            configuration.setClassLoaderForTemplateLoading(this.getClass().getClassLoader(), "templates");
            Template template = configuration.getTemplate(viewPath);
            StringWriter out = new StringWriter();
            // 渲染模板到输出流
            template.process(dataMap, out);
            String text = out.toString();
            this.send(tos, cc, subject, text, attachments);
        }catch (Exception var11) {
            throw new MailSendException("邮件发送失败！", var11);
        }
    }

}
